/*
 *  checkTest.c
 *  
 *
 *  Created by Rob Hole on 07/07/11.
 *  Copyright 2011 University of Victoria. All rights reserved.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#define SPACE 's'
#define RED 'r'
#define BLUE 'b'
#define GREEN 'g'

int columns;
int rows;
// NOTE: last_move starts at 1, not 0. This happens to be beneficial because, with padding, the first column is board[1]
int last_move;
int total_time;
int player_1_time;
int last_move_time;

int points[2000];

// (7,6,3,600,290,20,r,b,s,s,s,s,b,b,r,g,s,s,b,r,b,b,g,s,r,g,r,b,s,s,r,r,b,r,r,s,s,s,s,s,s,s,s,s,s,s,s,s)
int readBoard(char board[columns+2][rows+2])
{
    int i, j, p = 0;
	
    // Populate board array
    for (i = 0; i < columns+2; i++)
    {
        // Padding columns on left and right sides of board
        if (i == 0 || i == columns+1)
        {
            for(j = 0; j < rows+2; j++)
                board[i][j] = SPACE;
            continue;
        }
        // Padding Row over bottom of board
        board[i][0] = SPACE;
        for (j = 1; j < rows+1; j++)
        {
            scanf(",%c", &board[i][j]);
            if(board[i][j] < 's')
				p++;
        }
        // Padding row over top of board
        board[i][j] = SPACE;
    }
    return p;
}


void evalPoints2(char board[220*4])
{

	int i = 0; 
	
	for(i = 0; i < 220*4; i++)
	{
			//5 points
			if(board[i] == board[i+1] && board[i+2] == board[i+3])
			{
				if((board[i] == 'r' && board[i+2] == 'g') ||(  board[i+3] == 'r' && board[i] == 'g'))
					points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = -5;
				if((board[i] == 'b' && board[i+2] == 'g') ||(  board[i+3] == 'b' && board[i] == 'g'))
					points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 5;
			}
			if(board[i+1] == board[i+2])//potential 4 points
			{
				if(board[i] == 'r' && board[i+3] == 'r' && board[i+1] == 'g')
					points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = -4;
				if(board[i] == 'b' && board[i+3] == 'b' && board[i+1] == 'g')
					points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 4;
			}
			if((board[i] == board[i+2]) && (board[i+1] == board[i+3]))//3 points
			{
					if((board[i] == 'r' && board[i+1] == 'g') || (board[i] == 'g' && board[i+1] == 'r'))
					   points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = -3;
					if((board[i] == 'b' && board[i+1] == 'g') || (board[i] == 'g' && board[i+1] == 'b'))
						  points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 3;
			}
	}
}

void evalPoints(char board[220*4])
{

	int i = 0; 
	
	for(i = 0; i < 220*4; i++)
	{
			//5 points
			if(board[i] == board[i+1] && board[i+2] == board[i+3])
			{
				if((board[i] == 'r' && board[i+2] == 'g') ||(  board[i+3] == 'r' && board[i] == 'g'))
					points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = -5;
				if((board[i] == 'b' && board[i+2] == 'g') ||(  board[i+3] == 'b' && board[i] == 'g'))
					points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 5;
			}
			if(board[i+1] == board[i+2])//potential 4 points
			{
				if(board[i] == 'r' && board[i+3] == 'r' && board[i+1] == 'g')
					points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = -4;
				if(board[i] == 'b' && board[i+3] == 'b' && board[i+1] == 'g')
					points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 4;
			}
			if((board[i] == board[i+2]) && (board[i+1] == board[i+3]))//3 points
			{
					if((board[i] == 'r' && board[i+1] == 'g') || (board[i] == 'g' && board[i+1] == 'r'))
					   points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = -3;
					if((board[i] == 'b' && board[i+1] == 'g') || (board[i] == 'g' && board[i+1] == 'b'))
						  points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 3;
			}

			// Heuristic Scores (Non-wins, but still attractive)
			int k, greenCount = 0, blueCount = 0, redCount = 0, spaceCount = 0;
			for (k=0;k<4;k++)
			{
				if (board[i+k] == 'g')
					greenCount++;
				else if (board[i+k] == 'r')
					redCount++;
				else if (board[i+k] == 'b')
					blueCount++;
				else if (board[i+k] == 's')
					spaceCount++;
			}

			/* 1-Aways ***********************************/
			// Blue
			if (spaceCount == 1 && ((greenCount == 2 && blueCount == 1) || (greenCount == 1 && blueCount == 2)))
				points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 2;
			// Red
			else if (spaceCount == 1 && ((greenCount == 2 && redCount == 1) || (greenCount == 1 && redCount == 2)))
				points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = -2;

			/* 2-Aways ***********************************/
			// Blue	
			else if (spaceCount == 2 && ((greenCount == 1 && blueCount == 1) || blueCount == 2))
				points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 1;
			// Red						   
			else if (spaceCount == 2 && ((greenCount == 1 && redCount == 1) || redCount == 2))
				points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = -1;

			// TODO: What should a 2-Green 2-Space vector be scored? It can be beneficial for both players
			// For now, I'll leave it at 0 -Nick
			else if (spaceCount == 2 && greenCount == 2)
				points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 0;					
	}
	
}
	
	//(10,7,3,8561,2,1,g,g,b,b,g,b,b,g,b,g,b,g,b,g,b,b,g,g,b,b,g,b,b,g,r,r,b,r,r,b,r,r,g,b,r,b,r,b,s,g,r,s,s,g,b,b,s,r,r,g,s,g,s,r,r,s,g,r,g,r,s,b,g,g,b,s,s,s,s,s)
int main(void)
{
	int i,j;
	int p = 0;
	int x = 0;
	int col = -1;
	//scanf("(%d,%d,%d,%d,%d,%d", &columns, &rows, &last_move, &total_time, &player_1_time, &last_move_time);
	char board[220*4] = {
		's','s','s','s',
		's','s','s','b',
		's','s','s','g',
		's','s','s','r',
		's','s','b','s',
		's','s','b','b',
		's','s','b','g',
		's','s','b','r',
		's','s','g','s',
		's','s','g','b',
		's','s','g','g',
		's','s','g','r',
		's','s','r','s',
		's','s','r','b',
		's','s','r','g',
		's','s','r','r',
		's','b','s','s',
		's','b','s','b',
		's','b','s','g',
		's','b','s','r',
		's','b','b','s',
		's','b','b','b',
		's','b','b','g',
		's','b','b','r',
		's','b','g','s',
		's','b','g','b',
		's','b','g','g',
		's','b','g','r',
		's','b','r','r',
		's','g','s','s',
		's','g','s','b',
		's','g','s','g',
		's','g','s','r',
		's','g','b','s',
		's','g','b','b',
		's','g','b','g',
		's','g','b','r',
		's','g','g','r',
		's','g','r','r',
		's','r','s','s',
		's','r','s','b',
		's','r','s','g',
		's','r','s','r',
		's','r','b','s',
		's','r','b','b',
		's','r','b','g',
		's','r','b','r',
		's','r','g','s',
		's','r','g','b',
		's','r','g','g',
		's','r','g','r',
		's','r','r','s',
		's','r','r','b',
		's','r','r','g',
		's','r','r','r',
		'b','s','s','s',
		'b','s','s','b',
		'b','s','s','g',
		'b','s','s','r',
		'b','s','b','s',
		'b','s','b','b',
		'b','s','b','g',
		'b','s','b','r',
		'b','s','g','s',
		'b','s','g','b',
		'b','s','g','g',
		'b','s','g','r',
		'b','s','r','s',
		'b','s','r','b',
		'b','s','r','g',
		'b','s','r','r',
		'b','b','s','s',
		'b','b','s','b',
		'b','b','s','g',
		'b','b','s','r',
		'b','b','b','s',
		'b','b','b','b',
		'b','b','b','g',
		'b','b','b','r',
		'b','b','g','s',
		'b','b','g','b',
		'b','b','g','g',
		'b','b','g','r',
		'b','b','r','r',
		'b','g','s','s',
		'b','g','s','b',
		'b','g','s','g',
		'b','g','s','r',
		'b','g','b','s',
		'b','g','b','b',
		'b','g','b','g',
		'b','g','b','r',
		'b','g','g','r',
		'b','g','r','r',
		'b','r','s','s',
		'b','r','s','b',
		'b','r','s','g',
		'b','r','s','r',
		'b','r','b','s',
		'b','r','b','b',
		'b','r','b','g',
		'b','r','b','r',
		'b','r','g','s',
		'b','r','g','b',
		'b','r','g','g',
		'b','r','g','r',
		'b','r','r','s',
		'b','r','r','b',
		'b','r','r','g',
		'b','r','r','r',
		'g','s','s','s',
		'g','s','s','b',
		'g','s','s','g',
		'g','s','s','r',
		'g','s','b','s',
		'g','s','b','b',
		'g','s','b','g',
		'g','s','b','r',
		'g','s','g','s',
		'g','s','g','b',
		'g','s','g','g',
		'g','s','g','r',
		'g','s','r','s',
		'g','s','r','b',
		'g','s','r','g',
		'g','s','r','r',
		'g','b','s','s',
		'g','b','s','b',
		'g','b','s','g',
		'g','b','s','r',
		'g','b','b','s',
		'g','b','b','b',
		'g','b','b','g',
		'g','b','b','r',
		'g','b','g','s',
		'g','b','g','b',
		'g','b','g','g',
		'g','b','g','r',
		'g','b','r','r',
		'g','g','s','s',
		'g','g','s','b',
		'g','g','s','g',
		'g','g','s','r',
		'g','g','b','s',
		'g','g','b','b',
		'g','g','b','g',
		'g','g','b','r',
		'g','g','g','r',
		'g','g','r','r',
		'g','r','s','s',
		'g','r','s','b',
		'g','r','s','g',
		'g','r','s','r',
		'g','r','b','s',
		'g','r','b','b',
		'g','r','b','g',
		'g','r','b','r',
		'g','r','g','s',
		'g','r','g','b',
		'g','r','g','g',
		'g','r','g','r',
		'g','r','r','s',
		'g','r','r','b',
		'g','r','r','g',
		'g','r','r','r',
		'r','s','s','s',
		'r','s','s','b',
		'r','s','s','g',
		'r','s','s','r',
		'r','s','b','s',
		'r','s','b','b',
		'r','s','b','g',
		'r','s','b','r',
		'r','s','g','s',
		'r','s','g','b',
		'r','s','g','g',
		'r','s','g','r',
		'r','s','r','s',
		'r','s','r','b',
		'r','s','r','g',
		'r','s','r','r',
		'r','b','s','s',
		'r','b','s','b',
		'r','b','s','g',
		'r','b','s','r',
		'r','b','b','s',
		'r','b','b','b',
		'r','b','b','g',
		'r','b','b','r',
		'r','b','g','s',
		'r','b','g','b',
		'r','b','g','g',
		'r','b','g','r',
		'r','b','r','r',
		'r','g','s','s',
		'r','g','s','b',
		'r','g','s','g',
		'r','g','s','r',
		'r','g','b','s',
		'r','g','b','b',
		'r','g','b','g',
		'r','g','b','r',
		'r','g','g','r',
		'r','g','r','r',
		'r','r','s','s',
		'r','r','s','b',
		'r','r','s','g',
		'r','r','s','r',
		'r','r','b','s',
		'r','r','b','b',
		'r','r','b','g',
		'r','r','b','r',
		'r','r','g','s',
		'r','r','g','b',
		'r','r','g','g',
		'r','r','g','r',
		'r','r','r','s',
		'r','r','r','b',
		'r','r','r','g',
		'r','r','r','r'
	};
		
	evalPoints2(board);
			//p = readBoard(board);
	
	for(i = 0; i < 4*220; i+=4)
	{
		
		x = 0;
		x = (board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3];

		printf("P is:%d -> %c%c%c%c with score: %i\n", x, board[i], board[i+1], board[i+2], board[i+3], points[x]);
	//	printf("Pnew: %d\n", x&0xFF);
		
	}
		char vector1[4] = {'s', 's', 's', 'g'}; 
		char vector2[4] = {'r', 'r', 's', 's'};
		int score1 = (vector1[0] << 3) + (vector1[1] << 2) + (vector1[2] << 1) + vector1[3];
		int score2 = (vector2[0] << 3) + (vector2[1] << 2) + (vector2[2] << 1) + vector2[3];
		//printf("!!!! SCORE1 = %i and SCORE2 = %i\n", score1, score2);
	
	for(i = 0; i < 2000; i+=1)
	{
		

		
		printf("%d,", points[i]);
		//	printf("Pnew: %d\n", x&0xFF);
		
		
		
	}
	//printf("\n\n%d\n", points[1530]);
	
	
	return 0;

}
